<?php
	echo "<h2 id='contentTitle'>".$pageTitle."</h2>";
	echo $this->renderElement('message');

	echo '<div> Comunique-se! Envie e receba mensagens de pessoas pertencentes a sua entidade.' .
	'<br>	Em breve você poderá trocar mensagens com usuários de outras entidades! <br><br> </div>';

	$sendButton = "<span class='buttonSend'>&nbsp;&nbsp;&nbsp;Escrever mensagem</span>";
	echo html_entity_decode($html->link($sendButton, '/messages/send/',array('class'=>'button')));

	echo '';

	if (isset($messages) && is_array($messages) && (!empty($messages))) {
		echo "<table >";
		/* Cabeçalho */
		echo "<thead>";
		echo "<div></div>";
		echo "<tr class='tableHeader'>";
		echo "<th class='tableHeaderBegin'></th>";
		echo "<th class='tableHeaderMiddle'>Assunto</th>";
		echo "<th class='tableHeaderMiddle'>Remetente</th>";
		echo "<th class='tableHeaderMiddle'>Data</th>";
		echo "<th class='tableHeaderMiddle'>Mensagem</th>";
		echo "<th class='tableHeaderEnd'>Excluir</th>";

		echo "</tr>";
		echo "</thead>";

		/* Rodapé */
		echo "<tfoot>";
		echo "<tr class='tableFooter'>
		<td colspan='100%'></td>
		</tr>";
		echo "</tfoot>";

		/* Registros */
		$i=0;
		foreach ($messages as $message) {
			if (strlen($message['Message']['text']) >= 20) {
				$abstract = substr($message['Message']['text'],0, 20)."...";
			} else {
				$abstract = $message['Message']['text'];
			}
			if (($i++ % 2) == 1) $zebrado = " class='oddTr' ";
			else $zebrado = "";
			echo "<tr ".$zebrado.">";
			echo "<td>";


			if ($message['Message']['read']==0)
			{
				echo $html->image('mail/mail_unread.png', array('class'=>'mail'));
			}
			else if (!empty($message['Replied']))
			{
				echo $html->image('mail/mail_reply.png', array('class'=>'mail'));
			}
			else
			{
				echo $html->image('mail/mail_read.png', array('class'=>'mail'));
			}

			echo "</td>";
			echo "<td>".$html->link($message['Message']['subject'],'/messages/view/'.$message['Message']['id'])."</td>";
			echo "<td>".$html->link($message['From']['name'],'/users/view/'.$message['From']['id'])."</td>";
			echo "<td><small>".date('d/m/Y - G:i',strtotime($message['Message']['created']))."</small></td>";
			echo "<td><small>".$abstract."</small></td>";
			echo "<td class='tdCenter'>".html_entity_decode($html->link($html->image('filedel.png',array('class'=>'icon')), '/messages/delete/'.$message['Message']['id'], array(), 'Confirma a exclusão deste item?'))."</td>";
			echo "</tr>";
		}
		echo "</table>";
	} else {
		echo "<p>Não nenhuma mensagem.</p><br/>";
	}